<?php


namespace app\common\command;


use app\api\controller\Drillmatch;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use app\api\controller\Contest;
use app\api\controller\Yacht;
use think\Exception;
use think\Log;

class Timer extends Command
{
    /**
     * 配置指令参数
     */
    public function configure()
    {
        $this->setName('api:timer');
        $this->addArgument('action', Argument::OPTIONAL, 'stop|start|status|yachtstart|drillstart', 'status');
        $this->addArgument('md_id', Argument::OPTIONAL, 'Match Detail ID');
        $this->addArgument('mtr_id', Argument::OPTIONAL, 'Match Turns Rounds ID');
        $this->addArgument('dt_id', Argument::OPTIONAL, 'Drill Match Turns ID');
        $this->setDescription('比赛定时器');
    }

    /**
     * 执行指令内容
     * @param Input $input
     * @param Output $output
     * @return mixed
     */
    public function execute(Input $input, Output $output)
    {
        $action = $input->getArgument('action');
        if (method_exists($this, $method = "{$action}Action")) return $this->$method();
        $this->output->error("Wrong operation, Allow stop|start|yachtstart|status|drillstart");
    }
    
    /**
     * 帆船比赛
     */
    public function startAction()
    {

        $exec_data = $this->queue->data;
        $contest = new Contest();


        while (1) {
            $match_detail = Db::table('up__match_detail')->find($exec_data['md_id']);
            if ($match_detail) {
                if ($match_detail['end_time']) {
                    break;
                } else {
                    if ($match_detail['start_time']) {
                        $this->setQueueProgress("比赛进行中".microtime(true));
                    } else {
                        $this->setQueueProgress("比赛开始");
                        Db::table('up__match_detail')->where('id', $exec_data['md_id'])->update(['start_time' => microtime(true)]);
                    }
                    //TODO 数据采集业务镶嵌
                    try {

                        $res =  $contest->calculateDatas($exec_data['md_id']);
                    }catch (Exception $error){
                        $this->setQueueError($error->getFile().$error->getLine().$error->getMessage());
                        //log() ::ERROR($error->getFile().$error->getLine().$error->getMessage());
                        Log::error('Timer-->'.$error->getFile().$error->getLine().$error->getMessage());
                    }
                }
            } else {
                $this->setQueueError("比赛轮次[{$exec_data['md_id']}]未找到");
            }
            usleep(1000000);
        }

        $diff = $match_detail['end_time'] - $match_detail['start_time'];
        $this->setQueueSuccess('比赛结束,持续时长: ' . $diff . ' s');

    }
    
    /**
     * 赛艇比赛
     */
    public function yachtstartAction()
    {

        $exec_data = $this->queue->data;
        $yacht = new Yacht();


        while (1) {
            $match_turns_rounds = Db::table('up__match_turns_rounds')->where('id', $exec_data['mtr_id'])->find();
            if ($match_turns_rounds) {
                if ($match_turns_rounds['end_time']) {
                    break;
                } else {
                    if ($match_turns_rounds['start_time']) {
                        $this->setQueueProgress("比赛进行中".microtime(true));
                    } else {
                        $this->setQueueProgress("比赛开始");
                        Db::table('up__match_turns_rounds')->where('id', $exec_data['mtr_id'])->update(['start_time' => microtime(true)]);
                    }
                    //TODO 数据采集业务镶嵌
                    try {

                        $res =  $yacht->calculateDatas($exec_data['mtr_id']);
                    }catch (Exception $error){
                        $this->setQueueError($error->getFile().$error->getLine().$error->getMessage());
                        //log() ::ERROR($error->getFile().$error->getLine().$error->getMessage());
                        Log::error('Timer-->'.$error->getFile().$error->getLine().$error->getMessage());
                    }
                }
            } else {
                $this->setQueueError("赛艇比赛轮次[{$exec_data['mtr_id']}]未找到");
            }
            usleep(1000000);
        }

        $diff = $match_turns_rounds['end_time'] - $match_turns_rounds['start_time'];
        $this->setQueueSuccess('比赛结束,持续时长: ' . $diff . ' s');

    }
    
    /**
     * 训练赛
     */
    public function drillstartAction()
    {
        
        $exec_data = $this->queue->data;
        $drillmatch = new Drillmatch();
        
        
        while (1) {
            $sailboat_drill_turns = Db::table('up_sailboat_drill_turns')->where('dt_id', $exec_data['dt_id'])->find();
            if ($sailboat_drill_turns) {
                if ($sailboat_drill_turns['dt_end_time']) {
                    break;
                } else {
                    if ($sailboat_drill_turns['dt_start_time']) {
                        $this->setQueueProgress("训练进行中".microtime(true));
                    } else {
                        $this->setQueueProgress("训练开始");
                        Db::table('up_sailboat_drill_turns')->where('dt_id', $exec_data['dt_id'])->update(['dt_start_time' => microtime(true)]);
                    }
                    //TODO 数据采集业务镶嵌
                    try {
                        
                        $res =  $drillmatch->calculateDatas($exec_data['dt_id']);
                    }catch (Exception $error){
                        $this->setQueueError($error->getFile().$error->getLine().$error->getMessage());
                        //log() ::ERROR($error->getFile().$error->getLine().$error->getMessage());
                        Log::error('Timer-->'.$error->getFile().$error->getLine().$error->getMessage());
                    }
                }
            } else {
                $this->setQueueError("训练赛赛轮次[{$exec_data['dt_id']}]未找到");
            }
            usleep(1000000);
        }
        
        $diff = $sailboat_drill_turns['dt_end_time'] - $sailboat_drill_turns['dt_start_time'];
        $this->setQueueSuccess('训练结束,持续时长: ' . $diff . ' s');
        
    }

    public function stopAction()
    {

    }

    public function statusAction()
    {

    }
}